<?php
/*
# ------------------------------------------------------------------------
# JA Teline III - Version 1.3 - Licence Owner jSharing.COM
# ------------------------------------------------------------------------
# Copyright (C) Lifetime Joomla Just for Sharing, Ltd. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: Joomla Just for Sharing
# Websites:  http://jsharing.com -  http://jsharing.net
# This file may not be redistributed in whole or significant part.
# ------------------------------------------------------------------------
*/

// Prevent direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class JAPopupHelper extends JObject {
	// Modal name
	var $_modal_name;
	
	// Plugin params
	var $_pluginParams;
	
	// Param in {japopup} tag
	var $_tagParams;
	
	// Constructor for PHP 5
	function __construct($modalName, $pluginParams){
		$this->_modal_name = $modalName;
		$this->_pluginParams = $pluginParams;
	}
	
	/**
	 * Get paramater value accross key
	 * @param string $key Key of array
	 * @return string Value of key.
	 * */
	function getValue($key, $defaultValue=""){			
		if(isset($this->_tagParams[$key])){
			return $this->_tagParams[$key];
		}else {				
			return $defaultValue;
		}
	}
	
	// Return common value in {japopup} tag and config file
	function getCommonValue($arrJAPopupTag, $innerContent){
		$this->_tagParams = $arrJAPopupTag;
		// Content
		$modalContent 	= $this->getValue("content", "");
		$modalContent	= str_replace("&amp;", "&", $modalContent);
		$modalContent	= str_replace("&", "&amp;", $modalContent);
		// Title
		$modalTitle 	= $this->_pluginParams->get("add_title", "1");
		$modalTitle		= ($modalTitle != "0")?$this->getValue("title"):"";
		
		$arrData = array(
				"frameWidth"	=> $this->getValue("width"),
				"frameHeight"	=> $this->getValue("height"),
				"content"		=> html_entity_decode($innerContent),
				"title"			=> $modalTitle,
				"href"			=> $modalContent
			);
		return $arrData;
	}
	
	// Show data in template file
	function showDataInTemplate($modal="fancybox", $type="ajax", $arrData){
		$tmpFile = dirname( __FILE__ ).DS.$modal.DS.'tmpl'.DS.$type.".php";		
		if(file_exists($tmpFile)){
			ob_start();
			require($tmpFile);
			$content = ob_get_contents();
			ob_end_clean();
			return $content;
		}else{
			return "Template file not found.";
		}
	}
	
	function getLayoutPath($plugin, $layout = 'default')
	{
		global $mainframe;

		// Build the template and base path for the layout
		$tPath = JPATH_BASE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.$plugin->name.DS.$layout.'.php';
		$bPath = JPATH_BASE.DS.'plugins'.DS.$plugin->type.DS.$plugin->name.DS.'tmpl'.DS.$layout.'.php';
		// If the template has a layout override use it
		if (file_exists($tPath)) {
			return $tPath;
		} elseif (file_exists($bPath)) {
			return $bPath;
		}
		return '';
	}

	function loadLayout ($plugin, $layout = 'default') {
		$layout_path = $this->getLayoutPath ($plugin, $layout);
		if ($layout_path) {
			ob_start();
			require $layout_path;
			$content = ob_get_contents();
			ob_end_clean();
			return $content;
		}
		return '';
	}
	
	// Return header library 
	function getHeaderLibrary($bodyString, $identifierString, $headerString){
		if ( strpos( $bodyString, $identifierString ) === false  ) {		
			$bodyString = str_replace( '</head>', "\t".implode("\n", $headerString)."\n</head>", $bodyString );
		}
		return $bodyString;
	}
	
	// Return user agent
	function get_user_browser(){
		$u_agent = $_SERVER['HTTP_USER_AGENT'];
		$ub = '';
		if(preg_match('/MSIE/i',$u_agent)){
			$ub = "ie";
		}elseif(preg_match('/Firefox/i',$u_agent)){
			$ub = "firefox";
		}elseif(preg_match('/Safari/i',$u_agent)){
			$ub = "safari";
		}elseif(preg_match('/Chrome/i',$u_agent)){
			$ub = "chrome";
		}elseif(preg_match('/Flock/i',$u_agent)){
			$ub = "flock";
		}elseif(preg_match('/Opera/i',$u_agent)){
			$ub = "opera";
		}
		return $ub;
	}
}

?>